import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:device_info/device_info.dart';
import 'package:uuid/uuid.dart';

class DeviceHelper {
  static const String DEVICE_ID_KEY = "DEVICE_ID_KEY";

  static Future<String> getDeviceName() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      var androidInfo = await deviceInfo.androidInfo;
      return "${androidInfo.model}[sdk_${androidInfo.version.sdkInt}]";
    } else if (Platform.isIOS) {
      var iosInfo = await deviceInfo.iosInfo;
      return "${iosInfo.name}[${iosInfo.systemVersion}]";
    }
    return "Unknow Device";
  }

  static Future<String> getDeviceId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String deviceId = "";
    if (prefs.containsKey(DEVICE_ID_KEY)) {
      deviceId = prefs.getString(DEVICE_ID_KEY);
    }
    if (deviceId.isEmpty) {
      var uuid = new Uuid();
      deviceId = uuid.v4();
      prefs.setString(DEVICE_ID_KEY, deviceId);
    }
    return deviceId;
  }
}
